package com.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
public class PhoneValueController {

    @Autowired
    private JedisPool jedisPool;



    /*验证码过期时间*/
    static long PHONE_NUMBER_CODE_EXPIRE_SECONDS = 60;

    /*保护key的过期时间,5分钟*/
    static long PHONE_PROTECT_CODE_EXPIRE_SECONDS = 300;

    /*手机保护模式，IP锁定12小时*/
    static long PHONE_CODE_LOCK_IP_EXPIRE_SECONDS = 60*60*12;
    /**
     * 获取访问ip地址
     */
    public String getAccessIP(HttpServletRequest request){
        return request.getRemoteAddr();
    }
    /**
     * 生成验证码四位（调用手机号API短信接口）
     */
    public int generatorCode(){
        int num= (int) (Math.random()*10000);
        return num;
    }
    /**
     * 获取验证码请求
     *
     */
    @GetMapping("getVlidateCode")
    public Object getVlidateCode(@RequestParam("phoneNumber") String phoneNumber,HttpServletRequest request){

        String key = "phone:" + phoneNumber;
        //得到jedis连接
        Jedis jedis = jedisPool.getResource();

        //全局key判断当前操作ip是否被锁定
        /*IP锁定12小时*/
        String phoneCodeIPLockKey = "phone:code:lock:" + this.getAccessIP(request);
        if (jedis.exists(phoneCodeIPLockKey)){
            return "校验次数超过三次，一杯禁用，请12小时后再来";
        }

        /**
         * 保护模式（手机短信资费）
         * 1.生成保护模式key phone:code:ip
         * 2.判断 保护key 是否存在
         * 如果不存在，进行+1 并设定过期时间5分钟
         * 如果存在，进行+1
         * 3.判断保护key的值是否大于3，如果大于，生成一个新的锁Ikey: phone:code:lock:ip 过期时间为：12小时
         */

        String protectKey = "phone:code:" + this.getAccessIP(request);
        if (!jedis.exists(protectKey)){
            jedis.incr(protectKey);
            jedis.expire(phoneNumber,PHONE_PROTECT_CODE_EXPIRE_SECONDS);
        }else {
            jedis.incr(protectKey);
        }
        if (Integer.parseInt(jedis.get(protectKey))>3){
            jedis.set(phoneCodeIPLockKey,phoneCodeIPLockKey);
            jedis.expire(phoneCodeIPLockKey,PHONE_CODE_LOCK_IP_EXPIRE_SECONDS );
            return "校验次数超过三次，一杯禁用，请12小时后再来";
        }

        if (!jedis.exists(key)){
            int phoneCode = this.generatorCode();
            log.info("手机号已发送api短信接口，验证码是："+phoneCode);
            jedis.set(key,phoneCode+"");
            jedis.expire(key,PHONE_NUMBER_CODE_EXPIRE_SECONDS);
            return "验证码发送成功，请查看短信";
        }else {
            return "验证码获取失败，避免重复获取，请耐心等待，还剩时间："+jedis.ttl(key) + "秒";
        }
    }

    /**
     * 确定按钮，请求验证码和redis中验证码比较
     */
    @GetMapping("validate")
    public Object validate(@RequestParam("code") String code,@RequestParam("phoneNumber") String phoneNumber){

        Jedis jedis = jedisPool.getResource();

        String key = "phone:" + phoneNumber;
        if (code.equals(jedis.get(key))){
            log.info("手机验证码验证成功，执行登录确定");
            //清除redis中对应的key
            jedis.del(key);
            return "验证码验证成功";
        }else {
            log.info("验证码验证失败");
            return "验证码验证失败";
        }
    }
}
